package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.dto.device.DeviceReqDto;
import com.csun.cmny.provider.service.DmcDeviceAlertFeignApi;
import com.csun.cmny.provider.service.DmcDeviceFeignApi;
import com.csun.cmny.provider.service.DmcRpcService;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2018-12-12 17:05
 * @Description:
 */
@Slf4j
@Service
public class DmcRpcServiceImpl implements DmcRpcService {

    @Resource
    private DmcDeviceFeignApi dmcDeviceFeignApi;

    @Resource
    private DmcDeviceAlertFeignApi dmcDeviceAlertFeignApi;

    /**
     * 根据设备Id查询设备信息
     *
     * @param deviceId 设备Id
     *
     * @return 设备信息
     */
    @Override
    public DeviceReqDto getDeviceById(Long deviceId) {
        try {
            Wrapper<DeviceReqDto> wrapper = dmcDeviceFeignApi.getDeviceById(deviceId);

            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.MDC10021002);
            }

            DeviceReqDto result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("getDeviceById = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 发送阈值保存通知
     *
     * @param tagList
     */
    @Override
    public void sendThresholdSaveMsg(List<String> tagList) {

        try {
            Wrapper<Integer> wrapper = dmcDeviceAlertFeignApi.sendThresholdSaveMsg(tagList);

            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610055);
            }

        } catch (Exception e) {
            log.error("getDeviceById = {}", e.getMessage(), e);
        }
    }
}
